/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ProcessData;

import edu.uci.ics.jung.graph.util.EdgeType;
import edu.uci.ics.jung.graph.util.Pair;
import java.io.BufferedReader;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Iterator;
import org.javatuples.Tuple;
import pw.analyzer.Beacon;
import pw.analyzer.Edge;
import pw.analyzer.Node;

/**
 *
 * @author Heitor
 */
public class DataFile {
    
    private String nome;
    private ArrayList<Node> listNode;
    private ArrayList<Edge> listEdge;
    private ArrayList<ArrayList<Beacon>> matrix;

    //Variaveis auxiliares para a leitura de arquivo
    private String linha;
    private int nTesteAtual, nEtapaAtual;
    
    public DataFile() {
        nome = new String();
        listNode = new ArrayList<Node>();
        listEdge = new ArrayList<Edge>();
    }
    
    public DataFile(String diretorio) {
        
        //String[] x = diretorio.split("\\");
        //System.out.println(x.length);
        //int y = x.length - 1;
        nome = diretorio;
        listNode = new ArrayList<Node>();
        listEdge = new ArrayList<Edge>();
        
        readFile(diretorio);
    }
    
    private void readFile(String diretorio){
    
        int aux;
        
        Path path = Paths.get(diretorio);
        Charset utf8 = StandardCharsets.UTF_8;
        
        BufferedReader reader = null;
        
        try{
            reader = Files.newBufferedReader(path, utf8);
            linha = null;
            aux = 0;
            matrix = new ArrayList<ArrayList<Beacon>>();
            int f = 0; //Variavel para auxiliar no processo de matrix triangular
            while((linha = reader.readLine()) != null){
                processMatrix(aux, f);
                aux++;
                f++;
            }
            //printMatrix();
            
            processGraph();
        }catch(IOException e){
            e.printStackTrace();
        }

        
    }
    
    private void processMatrix(int aux, int f){

        String[] aux1;
        aux1 = linha.split(",");
        
        ArrayList<Beacon> aux2 = new ArrayList<Beacon>();
        
        for(int i=0+f; i<aux1.length; i++){
            Beacon beacon = new Beacon(i, false);
            switch (aux1[i]) {
                case "1":
                    beacon.setBool(true);
                    beacon.setValue("0");
                    aux2.add(beacon);
                    break;
                case "0":
                    beacon.setBool(false);
                    beacon.setValue("0");
                    aux2.add(beacon);
                    break;
                default:
                    beacon.setBool(true);
                    beacon.setValue(aux1[i]);
                    aux2.add(beacon);
                    break;
            }
        }
        matrix.add(aux2);
    }
    
    private void printMatrix(){
    
        for(Iterator i = matrix.iterator(); i.hasNext();){
            ArrayList<Boolean> x = (ArrayList<Boolean>)i.next();
            
            for(Iterator j = x.iterator(); j.hasNext();){
                Beacon y = (Beacon)j.next();
                
                if(y.isBool()){
                    System.out.print(y.getValue() + "(" + y.getId() + ") ");
                }else{
                    System.out.print(y.getValue() + "(" + y.getId() + ") ");
                }
            }
            System.out.println();
        }
    }
    
    private void processGraph(){
    
        /*
         * 
         * Criar todos os vertex(NODE.java) e edges(EDGE.java) para adicionar nas listas, e então colocar no grafo.
         * 
         */
        int cont_idNode = 0;
        int cont_idEdge = 0;
        
        for(int j=0; j<matrix.size(); j++){
           Node aux1 = new Node(cont_idNode, 2, cont_idNode);
           aux1.setNome("Nó " + cont_idNode);
           aux1.setDescricao("");
           listNode.add(aux1);
           cont_idNode++;
        }
        
        int cont_aux = 0;
        for(Iterator i = matrix.iterator(); i.hasNext();){
            ArrayList<Beacon> x = (ArrayList<Beacon>)i.next();
            
            for(Iterator j = x.iterator(); j.hasNext();){
                Beacon y = (Beacon)j.next();
                
                if(y.isBool()){
                    Edge edge = new Edge("Edge " + cont_aux, cont_idEdge, y.getId());
                    edge.setValue(y.getValue());
                    listEdge.add(edge);
                    cont_aux++;
                }
            }
            cont_idEdge++;
        }
        
    };

    /**
     * @return the listNode
     */
    public ArrayList<Node> getListNode() {
        return listNode;
    }

    /**
     * @param listNode the listNode to set
     */
    public void setListNode(ArrayList<Node> listNode) {
        this.listNode = listNode;
    }

    /**
     * @return the listEdge
     */
    public ArrayList<Edge> getListEdge() {
        return listEdge;
    }

    /**
     * @param listEdge the listEdge to set
     */
    public void setListEdge(ArrayList<Edge> listEdge) {
        this.listEdge = listEdge;
    }

    /**
     * @return the nome
     */
    public String getNome() {
        return nome;
    }

    /**
     * @param nome the nome to set
     */
    public void setNome(String nome) {
        this.nome = nome;
    }
    
}
